1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import com.google.caliper.Benchmark;
20  import com.google.common.base.Objects;
21  
22  /**
23   * Some microbenchmarks for the {@link Objects} class.
24   *
25   * @author Ben L. Titzer
26   */
27  public class ObjectsBenchmark {
28  
29    private static final Integer I0 = -45;
30    private static final Integer I1 = -1;
31    private static final Integer I2 = 3;
32    private static final String S0 = "3";
33    private static final String S1 = "Ninety five";
34    private static final String S2 = "44 one million";
35    private static final String S3 = "Lowly laundry lefties";
36    private static final String S4 = "89273487U#*&#";
37    private static final Double D0 = 9.234d;
38    private static final Double D1 = -1.2e55;
39  
40    @Benchmark int hashString_2(int reps) {
41      int dummy = 0;
42      for (int i = 0; i < reps; i++) {
43        dummy += Objects.hashCode(S0, S1);
44      }
45      return dummy;
46    }
47  
48    @Benchmark int hashString_3(int reps) {
49      int dummy = 0;
50      for (int i = 0; i < reps; i++) {
51        dummy += Objects.hashCode(S0, S1, S2);
52      }
53      return dummy;
54    }
55  
56    @Benchmark int hashString_4(int reps) {
57      int dummy = 0;
58      for (int i = 0; i < reps; i++) {
59        dummy += Objects.hashCode(S0, S1, S2, S3);
60      }
61      return dummy;
62    }
63  
64    @Benchmark int hashString_5(int reps) {
65      int dummy = 0;
66      for (int i = 0; i < reps; i++) {
67        dummy += Objects.hashCode(S0, S1, S2, S3, S4);
68      }
69      return dummy;
70    }
71  
72    @Benchmark int hashMixed_5(int reps) {
73      int dummy = 0;
74      for (int i = 0; i < reps; i++) {
75        dummy += Objects.hashCode(I2, S1, D1, S2, I0);
76        dummy += Objects.hashCode(D0, I1, S3, I2, S0);
77      }
78      return dummy;
79    }
80  }